<?php

namespace App\Http\Controllers\Api\v1;

use App\common;
use App\Http\Controllers\Controller;
use App\Logic\HouseReCometLogic;
use App\Logic\HouseReLogic;
use App\Models\House;
use App\Models\RentingModel;
use App\Models\users;
use Illuminate\Http\Request;
use function Swoole\command;

class HouseReComet extends Controller
{


    /**
     * @name wl
     * @function 房屋推荐列表
     * @param Request $request
     */
    public function houseReCometList(Request $request)
    {
        $user = $request->user(); //获取当前用户
        $HouseLogic = (new users())->interests()->pluck('action_type'); // 取出用户的兴趣列表
        $houses = House::query()->whereHas('tags', function ($query) use ($HouseLogic) {
            $query->whereIn('name', $HouseLogic);
        })
            ->orderBy('created_at', 'desc')
            ->take(20)
            ->get();
        return $houses;
    }


    /**
     * @name wl
     * @function  租房多条件搜索
     * @param Request $request
     */
    public function allSearch(Request $request)
    {
        try {
            $world = $request->all();
            if (!empty($world)) {
                if (isset($world['location']) && !empty($world['location'])) $query[] = ['match' => ['location' => $world['location']]];
                if (isset($world['price']) && !empty($world['price'])) $query[] = ['match' => ['price' => $world['price']]];
                $field = 'location';
                $data = HouseReCometLogic::EsMultipleSearch($query, $field);
                $data = json_decode(json_encode($data), true);
                return common::success(20000, $data, '搜索成功');
            }
            $res = ['id', 'title', 'location', 'price', 'description', 'create_time'];
            $room = RentingModel::paginate($res);
        } catch (\Exception $exception) {
            return common::fail(50000, [], '失败');
        }
    }

}
